package com.sykj.bsc.rwl.realm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.sykj.bsc.rwl.po.SysMenu;
import com.sykj.bsc.rwl.po.SysRole;
import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.rwl.po.extend.SysMenuExtend;
import com.sykj.bsc.rwl.service.SysUserService;
/**
 * 
 * @ClassName:  SysUserRealm   
 *@Description: 用户登录自定义realm
 * @author 任文龙
 ** @date:   2018年8月13日 下午5:04:16
 */
public class SysUserRealm extends AuthorizingRealm{
	@Autowired
	private SysUserService sysUserService;
	//认证
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String account=(String) token.getPrincipal();
		SysUser sysUser=null;
		try {
			sysUser=sysUserService.findByIdSysUser(account);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(sysUser==null) {
			return null;
		}else {
			if(!sysUser.getStatus().equals("1")) {
				 throw new LockedAccountException();
			}
		}
		String password=sysUser.getPassword();
		SimpleAuthenticationInfo info =  new SimpleAuthenticationInfo(account, password, ByteSource.Util.bytes(account), getName());
		return info;
	}
	
	//授权
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		String account=(String) principals.getPrimaryPrincipal();
		SysUser sysUser=null;
		List<String> list=new ArrayList<String>();
		try {
			sysUser=sysUserService.findByIdSysUser(account);
			if(sysUser!=null) {
				int user_id=sysUser.getUser_id();//获取用户id
				//通过用户id查询角色
				SysRole sysRole=sysUserService.findByIdSysRole(user_id);
				int role_id=sysRole.getRole_id();//获取角色id
				//通过角色id查询父节点权限信息
				List<SysMenuExtend> sysMenus=sysUserService.findByIdSysMenu(role_id);
				for(SysMenuExtend sysMenuExtend:sysMenus) {
					List<SysMenu> listMenu=sysMenuExtend.getSysMenus();
					for(SysMenu sysMenu:listMenu) {
						HashMap<String, Integer> map=new HashMap<>();
						int menu_id=sysMenu.getMenu_id();//获取子节点下的id
						map.put("menu_id", menu_id);
						map.put("role_id", role_id);
						List<SysMenu> list2=sysUserService.findByIdButton(map);
						for(SysMenu s:list2) {
							list.add(s.getAlias());
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.addStringPermissions(list);
		return info;
	}
}
